package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.mapper.*;
import com.heima.pojo.entity.*;
import com.heima.pojo.vo.NodeRegionBusinessVo;
import com.heima.pojo.vo.VmTypeNodeRegionVo;
import com.heima.pojo.vo.PageVo;
import com.heima.service.VendingMachineService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;

@Service
public class VendingMachineServiceImpl implements VendingMachineService {

    @Autowired
    private VendingMachineMapper vendingMachineMapper;
    @Autowired
    private VmTypeMapper vmTypeMapper;
    @Autowired
    private NodeMapper nodeMapper;
    @Autowired
    private RegionMapper regionMapper;
    @Autowired
    private BusinessMapper businessMapper;
    /**
     * 设备条件分页查询
     * @param pageIndex
     * @param pageSize
     * @param innerCode
     * @return
     */
    @Override
    public PageVo getVmPage(Integer pageIndex, Integer pageSize, String innerCode) {
        //先获取非集合的几个数据并进行封装
        PageVo<VmTypeNodeRegionVo> vmPageVo = new PageVo<>();
        List<VmTypeNodeRegionVo> vmTypeNodeRegionVos = new ArrayList<>();
        //进行分页和查询 得到vm集合
        PageHelper.startPage(pageIndex,pageSize);
        LambdaQueryWrapper<VendingMachine> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(innerCode!=null,VendingMachine::getInnerCode,innerCode);
        List<VendingMachine> vmList= vendingMachineMapper.selectList(queryWrapper);
        //将查询结果强转为page 并封装要返回的数据
        Page<VendingMachine> page = (Page<VendingMachine>) vmList;
        vmPageVo.setPageIndex(pageIndex);
        vmPageVo.setPageSize(pageSize);
        vmPageVo.setTotalCount(page.getTotal());
        vmPageVo.setTotalPage(page.getPages());
        //获取currentPageRecords集合
        for (VendingMachine vendingMachine:vmList) {
            VmTypeNodeRegionVo vmTypeNodeRegionVo = new VmTypeNodeRegionVo();
            BeanUtils.copyProperties(vendingMachine, vmTypeNodeRegionVo);
            //查询vm-type表
            LambdaQueryWrapper<TbVmType> vmType = new LambdaQueryWrapper<>();
            vmType.eq(TbVmType::getTypeId,vendingMachine.getVmType());
            TbVmType tbVmType = vmTypeMapper.selectOne(vmType);
            vmTypeNodeRegionVo.setType(tbVmType);
            //查询region表
            Region region = regionMapper.selectById(vendingMachine.getRegionId());
            vmTypeNodeRegionVo.setRegion(region);
            //查询currentPageRecords内容封装到nod对象
            //查询node
            Node node = nodeMapper.selectById(vendingMachine.getNodeId());
            NodeRegionBusinessVo nodeRegionBusinessVo = new NodeRegionBusinessVo();
            BeanUtils.copyProperties(node, nodeRegionBusinessVo);
            nodeRegionBusinessVo.setRegion(region);
            //查询business表
            Business business = businessMapper.selectById(vendingMachine.getBusinessId());
            nodeRegionBusinessVo.setBusinessType(business);
            //设置vmcode
            LambdaQueryWrapper<VendingMachine> qw = new LambdaQueryWrapper<>();
            qw.eq(VendingMachine::getNodeId,vendingMachine.getId());
            List<VendingMachine> vendingMachines = vendingMachineMapper.selectList(qw);
            nodeRegionBusinessVo.setVmCount(vendingMachines.size());
            //封装node
            vmTypeNodeRegionVo.setNode(nodeRegionBusinessVo);
            vmTypeNodeRegionVos.add(vmTypeNodeRegionVo);
        }
        //封装数据到返回对象
        vmPageVo.setCurrentPageRecords(vmTypeNodeRegionVos);
        return vmPageVo;
    }

    /**
     * 新增设备
     * @param vendingMachine
     * @return
     */
    @Override
    public Boolean save(VendingMachine vendingMachine) {
        //根据nodeid查询node
        Node node = nodeMapper.selectById(vendingMachine.getNodeId());
        vendingMachine.setOwnerId(node.getOwnerId());
        vendingMachine.setOwnerName(node.getOwnerName());
        vendingMachine.setAreaId(Integer.valueOf(node.getAreaCode()));
        //设置商圈id
        vendingMachine.setBusinessId(node.getBusinessId());
        //设置区域id
        vendingMachine.setRegionId(node.getRegionId());
        vendingMachine.setCreateTime(LocalDateTime.now());
        vendingMachine.setUpdateTime(LocalDateTime.now());
        vendingMachine.setVmStatus(0);
        vendingMachine.setLastSupplyTime(LocalDateTime.now());
        // 生成innerCode
        String innerCode = generateInnerCode();
        vendingMachine.setInnerCode(innerCode);

         return vendingMachineMapper.add(vendingMachine);
    }
    /**
     * 生成新的innerCode
     * 基于数据库中最大的innerCode值进行递增
     * @return 新的innerCode字符串
     */
    private String generateInnerCode() {
        // 查询数据库中最大的innerCode
        String maxInnerCode = vendingMachineMapper.selectMaxInnerCode();

        // 如果数据库中没有记录，从默认值开始
        if (maxInnerCode == null || maxInnerCode.isEmpty()) {
            return "01000066";
        }

        try {
            // 将字符串转换为数字并加1
            int nextCode = Integer.parseInt(maxInnerCode) + 1;
            // 保持格式一致，使用前导零填充到8位
            return String.format("%08d", nextCode);
        } catch (NumberFormatException e) {
            // 如果转换失败，使用默认值
            return "01000066";
        }
    }

}
